
import java.net.URL;
import java.util.LinkedList;


/**
 * Dispatches events added to the connectionList
 * @author hussein
 */
public class EventDispatcher extends Thread {

    public final LinkedList<EventConnection> connectionList;
    public HashMapCache<URL, byte[]> cache;

    public EventDispatcher(HashMapCache<URL, byte[]> cache, LinkedList<EventConnection> connectionList) {
        super();
        this.connectionList = connectionList;
        this.cache = cache;
    }

    @Override
    public void run() {
        while(true) {
            launchEvent();
        }
    }

    private void launchEvent() {
        synchronized (connectionList) {
            while (connectionList.size() == 0) {
                try {
                    connectionList.wait();
                } catch (InterruptedException ex) {
                    System.out.println(ex);
                }
            }
            EventWorker ew = new EventWorker(cache, connectionList, connectionList.removeFirst());
            ew.start();
        }
    }
}
